blob: 0f8fe57da20a64eede2a53ed0933c5e23aa8a2b7 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===--------------------------- future -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUTURE
12#define _LIBCPP_FUTURE
13
14/*
15 future synopsis
16
17namespace std
18{
19
20enum class future_errc
21{
Howard Hinnantcd942f12013-09-14 18:20:1022 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:1623 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:1024 no_state,
25 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:1626};
27
28enum class launch
29{
Howard Hinnant66895642010-11-23 18:33:5430 async = 1,
31 deferred = 2,
32 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:1633};
34
35enum class future_status
36{
37 ready,
38 timeout,
39 deferred
40};
41
42template <> struct is_error_code_enum<future_errc> : public true_type { };
Howard Hinnant8bf01dd2012-07-21 17:46:5543error_code make_error_code(future_errc e) noexcept;
44error_condition make_error_condition(future_errc e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1645
Howard Hinnant8bf01dd2012-07-21 17:46:5546const error_category& future_category() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1647
48class future_error
49 : public logic_error
50{
51public:
52 future_error(error_code ec); // exposition only
53
Howard Hinnant8bf01dd2012-07-21 17:46:5554 const error_code& code() const noexcept;
55 const char* what() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1656};
57
58template <class R>
59class promise
60{
61public:
62 promise();
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:5565 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1666 promise(const promise& rhs) = delete;
67 ~promise();
68
69 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:5570 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1671 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:5572 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1673
74 // retrieving the result
75 future<R> get_future();
76
77 // setting the result
78 void set_value(const R& r);
79 void set_value(R&& r);
80 void set_exception(exception_ptr p);
81
82 // setting the result with deferred notification
83 void set_value_at_thread_exit(const R& r);
84 void set_value_at_thread_exit(R&& r);
85 void set_exception_at_thread_exit(exception_ptr p);
86};
87
88template <class R>
89class promise<R&>
90{
91public:
92 promise();
93 template <class Allocator>
94 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:5595 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1696 promise(const promise& rhs) = delete;
97 ~promise();
98
99 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55100 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16101 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55102 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16103
104 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19105 future<R&> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16106
107 // setting the result
108 void set_value(R& r);
109 void set_exception(exception_ptr p);
110
111 // setting the result with deferred notification
112 void set_value_at_thread_exit(R&);
113 void set_exception_at_thread_exit(exception_ptr p);
114};
115
116template <>
117class promise<void>
118{
119public:
120 promise();
121 template <class Allocator>
122 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:55123 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16124 promise(const promise& rhs) = delete;
125 ~promise();
126
127 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55128 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16129 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55130 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16131
132 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19133 future<void> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16134
135 // setting the result
136 void set_value();
137 void set_exception(exception_ptr p);
138
139 // setting the result with deferred notification
140 void set_value_at_thread_exit();
141 void set_exception_at_thread_exit(exception_ptr p);
142};
143
Howard Hinnant8bf01dd2012-07-21 17:46:55144template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16145
146template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
148
149template <class R>
150class future
151{
152public:
Howard Hinnant8bf01dd2012-07-21 17:46:55153 future() noexcept;
154 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16155 future(const future& rhs) = delete;
156 ~future();
157 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55158 future& operator=(future&&) noexcept;
159 shared_future<R> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16160
161 // retrieving the value
162 R get();
163
164 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55165 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16166
167 void wait() const;
168 template <class Rep, class Period>
169 future_status
170 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171 template <class Clock, class Duration>
172 future_status
173 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
174};
175
176template <class R>
177class future<R&>
178{
179public:
Howard Hinnant8bf01dd2012-07-21 17:46:55180 future() noexcept;
181 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16182 future(const future& rhs) = delete;
183 ~future();
184 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55185 future& operator=(future&&) noexcept;
186 shared_future<R&> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16187
188 // retrieving the value
189 R& get();
190
191 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55192 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16193
194 void wait() const;
195 template <class Rep, class Period>
196 future_status
197 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198 template <class Clock, class Duration>
199 future_status
200 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
201};
202
203template <>
204class future<void>
205{
206public:
Howard Hinnant8bf01dd2012-07-21 17:46:55207 future() noexcept;
208 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16209 future(const future& rhs) = delete;
210 ~future();
211 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55212 future& operator=(future&&) noexcept;
213 shared_future<void> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16214
215 // retrieving the value
216 void get();
217
218 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55219 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16220
221 void wait() const;
222 template <class Rep, class Period>
223 future_status
224 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225 template <class Clock, class Duration>
226 future_status
227 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
228};
229
230template <class R>
231class shared_future
232{
233public:
Howard Hinnant8bf01dd2012-07-21 17:46:55234 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16235 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16238 ~shared_future();
239 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55240 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16241
242 // retrieving the value
243 const R& get() const;
244
245 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55246 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16247
248 void wait() const;
249 template <class Rep, class Period>
250 future_status
251 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252 template <class Clock, class Duration>
253 future_status
254 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
255};
256
257template <class R>
258class shared_future<R&>
259{
260public:
Howard Hinnant8bf01dd2012-07-21 17:46:55261 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16262 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16265 ~shared_future();
266 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55267 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16268
269 // retrieving the value
270 R& get() const;
271
272 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55273 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16274
275 void wait() const;
276 template <class Rep, class Period>
277 future_status
278 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279 template <class Clock, class Duration>
280 future_status
281 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
282};
283
284template <>
285class shared_future<void>
286{
287public:
Howard Hinnant8bf01dd2012-07-21 17:46:55288 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16289 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16292 ~shared_future();
293 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55294 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16295
296 // retrieving the value
297 void get() const;
298
299 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55300 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16301
302 void wait() const;
303 template <class Rep, class Period>
304 future_status
305 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306 template <class Clock, class Duration>
307 future_status
308 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
309};
310
Howard Hinnantbc8d3f92010-05-11 19:42:16311template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16313 async(F&& f, Args&&... args);
314
315template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23316 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16317 async(launch policy, F&& f, Args&&... args);
318
Howard Hinnantf5256e12010-05-11 21:36:01319template <class> class packaged_task; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16320
321template <class R, class... ArgTypes>
322class packaged_task<R(ArgTypes...)>
323{
324public:
325 typedef R result_type;
326
327 // construction and destruction
Howard Hinnant8bf01dd2012-07-21 17:46:55328 packaged_task() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16329 template <class F>
Howard Hinnantbc8d3f92010-05-11 19:42:16330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
Marshall Clow07546f32015-06-30 14:16:49332 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
Howard Hinnantbc8d3f92010-05-11 19:42:16333 ~packaged_task();
334
335 // no copy
Howard Hinnant8131a012012-07-21 19:34:12336 packaged_task(const packaged_task&) = delete;
337 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16338
339 // move support
Howard Hinnant8bf01dd2012-07-21 17:46:55340 packaged_task(packaged_task&& other) noexcept;
341 packaged_task& operator=(packaged_task&& other) noexcept;
342 void swap(packaged_task& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16343
Howard Hinnant8bf01dd2012-07-21 17:46:55344 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16345
346 // result retrieval
347 future<R> get_future();
348
349 // execution
350 void operator()(ArgTypes... );
351 void make_ready_at_thread_exit(ArgTypes...);
352
353 void reset();
354};
355
356template <class R>
Howard Hinnant8bf01dd2012-07-21 17:46:55357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16358
359template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
360
361} // std
362
363*/
364
365#include <__config>
366#include <system_error>
Howard Hinnant47499b12010-08-27 20:10:19367#include <memory>
368#include <chrono>
369#include <exception>
Howard Hinnante6e4d012010-09-03 21:46:37370#include <mutex>
Howard Hinnant47499b12010-08-27 20:10:19371#include <thread>
Howard Hinnantbc8d3f92010-05-11 19:42:16372
Howard Hinnant08e17472011-10-17 20:05:10373#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16374#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10375#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16376
Jonathan Roelofsbaed05d2014-09-05 20:28:44377#ifdef _LIBCPP_HAS_NO_THREADS
Jonathan Roelofs8d86b2e2014-09-05 19:45:05378#error <future> is not supported on this single threaded system
379#else // !_LIBCPP_HAS_NO_THREADS
380
Howard Hinnantbc8d3f92010-05-11 19:42:16381_LIBCPP_BEGIN_NAMESPACE_STD
382
383//enum class future_errc
Howard Hinnantf6d875f2011-12-02 19:36:40384_LIBCPP_DECLARE_STRONG_ENUM(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16385{
Howard Hinnantcd942f12013-09-14 18:20:10386 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:16387 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:10388 no_state,
389 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:16390};
Howard Hinnantf6d875f2011-12-02 19:36:40391_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16392
Howard Hinnant8c6cbb22010-09-22 14:16:26393template <>
Howard Hinnant0f678bd2013-08-12 18:38:34394struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc> : public true_type {};
Howard Hinnanta6521722010-08-25 17:32:05395
Howard Hinnantf6d875f2011-12-02 19:36:40396#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
397template <>
Howard Hinnant0f678bd2013-08-12 18:38:34398struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc::__lx> : public true_type { };
Howard Hinnantf6d875f2011-12-02 19:36:40399#endif
400
Howard Hinnantbc8d3f92010-05-11 19:42:16401//enum class launch
Howard Hinnantf6d875f2011-12-02 19:36:40402_LIBCPP_DECLARE_STRONG_ENUM(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16403{
Howard Hinnant66895642010-11-23 18:33:54404 async = 1,
405 deferred = 2,
406 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:16407};
Howard Hinnantf6d875f2011-12-02 19:36:40408_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16409
Howard Hinnantf491e512013-06-29 18:38:17410#ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
411
412#ifdef _LIBCXX_UNDERLYING_TYPE
413typedef underlying_type<launch>::type __launch_underlying_type;
414#else
415typedef int __launch_underlying_type;
416#endif
417
418inline _LIBCPP_INLINE_VISIBILITY
419_LIBCPP_CONSTEXPR
420launch
421operator&(launch __x, launch __y)
422{
423 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
424 static_cast<__launch_underlying_type>(__y));
425}
426
427inline _LIBCPP_INLINE_VISIBILITY
428_LIBCPP_CONSTEXPR
429launch
430operator|(launch __x, launch __y)
431{
432 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
433 static_cast<__launch_underlying_type>(__y));
434}
435
436inline _LIBCPP_INLINE_VISIBILITY
437_LIBCPP_CONSTEXPR
438launch
439operator^(launch __x, launch __y)
440{
441 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
442 static_cast<__launch_underlying_type>(__y));
443}
444
445inline _LIBCPP_INLINE_VISIBILITY
446_LIBCPP_CONSTEXPR
447launch
448operator~(launch __x)
449{
Howard Hinnant6a683bf2013-07-02 18:01:41450 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
Howard Hinnantf491e512013-06-29 18:38:17451}
452
453inline _LIBCPP_INLINE_VISIBILITY
454launch&
455operator&=(launch& __x, launch __y)
456{
457 __x = __x & __y; return __x;
458}
459
460inline _LIBCPP_INLINE_VISIBILITY
461launch&
462operator|=(launch& __x, launch __y)
463{
464 __x = __x | __y; return __x;
465}
466
467inline _LIBCPP_INLINE_VISIBILITY
468launch&
469operator^=(launch& __x, launch __y)
470{
471 __x = __x ^ __y; return __x;
472}
473
474#endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
475
Howard Hinnantbc8d3f92010-05-11 19:42:16476//enum class future_status
Howard Hinnantf6d875f2011-12-02 19:36:40477_LIBCPP_DECLARE_STRONG_ENUM(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16478{
Howard Hinnantbc8d3f92010-05-11 19:42:16479 ready,
480 timeout,
481 deferred
482};
Howard Hinnantf6d875f2011-12-02 19:36:40483_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16484
Howard Hinnant83eade62013-03-06 23:30:19485_LIBCPP_FUNC_VIS
Howard Hinnant8bf01dd2012-07-21 17:46:55486const error_category& future_category() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05487
488inline _LIBCPP_INLINE_VISIBILITY
489error_code
Howard Hinnant8bf01dd2012-07-21 17:46:55490make_error_code(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05491{
492 return error_code(static_cast<int>(__e), future_category());
493}
494
495inline _LIBCPP_INLINE_VISIBILITY
496error_condition
Howard Hinnant8bf01dd2012-07-21 17:46:55497make_error_condition(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05498{
499 return error_condition(static_cast<int>(__e), future_category());
500}
501
Howard Hinnant8c6cbb22010-09-22 14:16:26502class _LIBCPP_EXCEPTION_ABI future_error
Howard Hinnanta6521722010-08-25 17:32:05503 : public logic_error
504{
505 error_code __ec_;
506public:
507 future_error(error_code __ec);
508
Howard Hinnant8c6cbb22010-09-22 14:16:26509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55510 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnantac6de542011-07-07 21:03:52511
512 virtual ~future_error() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05513};
514
Marshall Clowa1899742015-09-03 15:11:32515template <future_errc _Ev>
516_LIBCPP_ALWAYS_INLINE
517void __throw_future_error()
518{
519#ifndef _LIBCPP_NO_EXCEPTIONS
520 throw future_error(make_error_code(_Ev));
521#else
522 assert(!"future_error");
523#endif
524}
525
Howard Hinnant0f678bd2013-08-12 18:38:34526class _LIBCPP_TYPE_VIS __assoc_sub_state
Howard Hinnant47499b12010-08-27 20:10:19527 : public __shared_count
528{
529protected:
530 exception_ptr __exception_;
531 mutable mutex __mut_;
532 mutable condition_variable __cv_;
533 unsigned __state_;
534
Howard Hinnant1694d232011-05-28 14:41:13535 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21536 void __sub_wait(unique_lock<mutex>& __lk);
Howard Hinnant47499b12010-08-27 20:10:19537public:
538 enum
539 {
540 __constructed = 1,
541 __future_attached = 2,
542 ready = 4,
543 deferred = 8
544 };
545
Howard Hinnant8c6cbb22010-09-22 14:16:26546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19547 __assoc_sub_state() : __state_(0) {}
548
Howard Hinnant8c6cbb22010-09-22 14:16:26549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19550 bool __has_value() const
551 {return (__state_ & __constructed) || (__exception_ != nullptr);}
552
Howard Hinnant8c6cbb22010-09-22 14:16:26553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1b031c92013-01-14 20:01:24554 void __set_future_attached()
555 {
556 lock_guard<mutex> __lk(__mut_);
557 __state_ |= __future_attached;
558 }
Howard Hinnant8c6cbb22010-09-22 14:16:26559 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45560 bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19561
Howard Hinnant8c6cbb22010-09-22 14:16:26562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21563 void __set_deferred() {__state_ |= deferred;}
564
Howard Hinnant47499b12010-08-27 20:10:19565 void __make_ready();
Howard Hinnant8c6cbb22010-09-22 14:16:26566 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45567 bool __is_ready() const {return (__state_ & ready) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19568
569 void set_value();
570 void set_value_at_thread_exit();
571
572 void set_exception(exception_ptr __p);
573 void set_exception_at_thread_exit(exception_ptr __p);
574
575 void copy();
576
Howard Hinnant54da3382010-08-30 18:46:21577 void wait();
Howard Hinnant47499b12010-08-27 20:10:19578 template <class _Rep, class _Period>
579 future_status
580 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
581 template <class _Clock, class _Duration>
582 future_status
583 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
Howard Hinnant54da3382010-08-30 18:46:21584
585 virtual void __execute();
Howard Hinnant47499b12010-08-27 20:10:19586};
587
Howard Hinnantf39daa82010-08-28 21:01:06588template <class _Clock, class _Duration>
589future_status
590__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
591{
592 unique_lock<mutex> __lk(__mut_);
Howard Hinnant54da3382010-08-30 18:46:21593 if (__state_ & deferred)
594 return future_status::deferred;
595 while (!(__state_ & ready) && _Clock::now() < __abs_time)
Howard Hinnantf39daa82010-08-28 21:01:06596 __cv_.wait_until(__lk, __abs_time);
597 if (__state_ & ready)
598 return future_status::ready;
Howard Hinnantf39daa82010-08-28 21:01:06599 return future_status::timeout;
600}
601
602template <class _Rep, class _Period>
603inline _LIBCPP_INLINE_VISIBILITY
604future_status
605__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
606{
Howard Hinnantf8f85212010-11-20 19:16:30607 return wait_until(chrono::steady_clock::now() + __rel_time);
Howard Hinnantf39daa82010-08-28 21:01:06608}
609
Howard Hinnant99968442011-11-29 18:15:50610template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19611class __assoc_state
612 : public __assoc_sub_state
613{
614 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50615 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
Howard Hinnant47499b12010-08-27 20:10:19616protected:
Howard Hinnant99968442011-11-29 18:15:50617 _Up __value_;
Howard Hinnant47499b12010-08-27 20:10:19618
Howard Hinnant1694d232011-05-28 14:41:13619 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19620public:
621
622 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19623#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19624 void set_value(_Arg&& __arg);
625#else
626 void set_value(_Arg& __arg);
627#endif
628
629 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19630#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19631 void set_value_at_thread_exit(_Arg&& __arg);
632#else
633 void set_value_at_thread_exit(_Arg& __arg);
634#endif
635
Howard Hinnant99968442011-11-29 18:15:50636 _Rp move();
637 typename add_lvalue_reference<_Rp>::type copy();
Howard Hinnant47499b12010-08-27 20:10:19638};
639
Howard Hinnant99968442011-11-29 18:15:50640template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19641void
Howard Hinnant99968442011-11-29 18:15:50642__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19643{
644 if (this->__state_ & base::__constructed)
Howard Hinnant99968442011-11-29 18:15:50645 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
Howard Hinnant47499b12010-08-27 20:10:19646 delete this;
647}
648
Howard Hinnant99968442011-11-29 18:15:50649template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19650template <class _Arg>
651void
Howard Hinnant73d21a42010-09-04 23:28:19652#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50653__assoc_state<_Rp>::set_value(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19654#else
Howard Hinnant99968442011-11-29 18:15:50655__assoc_state<_Rp>::set_value(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19656#endif
657{
658 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant47499b12010-08-27 20:10:19659 if (this->__has_value())
Marshall Clowa1899742015-09-03 15:11:32660 __throw_future_error<future_errc::promise_already_satisfied>();
Howard Hinnant99968442011-11-29 18:15:50661 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19662 this->__state_ |= base::__constructed | base::ready;
Howard Hinnant47499b12010-08-27 20:10:19663 __cv_.notify_all();
664}
665
Howard Hinnant99968442011-11-29 18:15:50666template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19667template <class _Arg>
668void
Howard Hinnant73d21a42010-09-04 23:28:19669#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50670__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19671#else
Howard Hinnant99968442011-11-29 18:15:50672__assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19673#endif
674{
675 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant47499b12010-08-27 20:10:19676 if (this->__has_value())
Marshall Clowa1899742015-09-03 15:11:32677 __throw_future_error<future_errc::promise_already_satisfied>();
Howard Hinnant99968442011-11-29 18:15:50678 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19679 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04680 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant47499b12010-08-27 20:10:19681}
682
Howard Hinnant99968442011-11-29 18:15:50683template <class _Rp>
684_Rp
685__assoc_state<_Rp>::move()
Howard Hinnant47499b12010-08-27 20:10:19686{
687 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21688 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19689 if (this->__exception_ != nullptr)
690 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50691 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
Howard Hinnant47499b12010-08-27 20:10:19692}
693
Howard Hinnant99968442011-11-29 18:15:50694template <class _Rp>
695typename add_lvalue_reference<_Rp>::type
696__assoc_state<_Rp>::copy()
Howard Hinnant47499b12010-08-27 20:10:19697{
698 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21699 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19700 if (this->__exception_ != nullptr)
701 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50702 return *reinterpret_cast<_Rp*>(&__value_);
Howard Hinnant47499b12010-08-27 20:10:19703}
704
Howard Hinnant99968442011-11-29 18:15:50705template <class _Rp>
706class __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06707 : public __assoc_sub_state
708{
709 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50710 typedef _Rp* _Up;
Howard Hinnantf39daa82010-08-28 21:01:06711protected:
Howard Hinnant99968442011-11-29 18:15:50712 _Up __value_;
Howard Hinnantf39daa82010-08-28 21:01:06713
Howard Hinnant1694d232011-05-28 14:41:13714 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06715public:
716
Howard Hinnant99968442011-11-29 18:15:50717 void set_value(_Rp& __arg);
718 void set_value_at_thread_exit(_Rp& __arg);
Howard Hinnantf39daa82010-08-28 21:01:06719
Howard Hinnant99968442011-11-29 18:15:50720 _Rp& copy();
Howard Hinnantf39daa82010-08-28 21:01:06721};
722
Howard Hinnant99968442011-11-29 18:15:50723template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06724void
Howard Hinnant99968442011-11-29 18:15:50725__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06726{
727 delete this;
728}
729
Howard Hinnant99968442011-11-29 18:15:50730template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06731void
Howard Hinnant99968442011-11-29 18:15:50732__assoc_state<_Rp&>::set_value(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06733{
734 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnantf39daa82010-08-28 21:01:06735 if (this->__has_value())
Marshall Clowa1899742015-09-03 15:11:32736 __throw_future_error<future_errc::promise_already_satisfied>();
Howard Hinnanta4e87ab2013-08-08 18:38:55737 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06738 this->__state_ |= base::__constructed | base::ready;
Howard Hinnantf39daa82010-08-28 21:01:06739 __cv_.notify_all();
740}
741
Howard Hinnant99968442011-11-29 18:15:50742template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06743void
Howard Hinnant99968442011-11-29 18:15:50744__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06745{
746 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnantf39daa82010-08-28 21:01:06747 if (this->__has_value())
Marshall Clowa1899742015-09-03 15:11:32748 __throw_future_error<future_errc::promise_already_satisfied>();
Howard Hinnanta4e87ab2013-08-08 18:38:55749 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06750 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04751 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnantf39daa82010-08-28 21:01:06752}
753
Howard Hinnant99968442011-11-29 18:15:50754template <class _Rp>
755_Rp&
756__assoc_state<_Rp&>::copy()
Howard Hinnantf39daa82010-08-28 21:01:06757{
758 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21759 this->__sub_wait(__lk);
Howard Hinnantf39daa82010-08-28 21:01:06760 if (this->__exception_ != nullptr)
761 rethrow_exception(this->__exception_);
762 return *__value_;
763}
764
Howard Hinnant99968442011-11-29 18:15:50765template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19766class __assoc_state_alloc
Howard Hinnant99968442011-11-29 18:15:50767 : public __assoc_state<_Rp>
Howard Hinnant47499b12010-08-27 20:10:19768{
Howard Hinnant99968442011-11-29 18:15:50769 typedef __assoc_state<_Rp> base;
Howard Hinnant47499b12010-08-27 20:10:19770 _Alloc __alloc_;
771
Howard Hinnant1694d232011-05-28 14:41:13772 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19773public:
Howard Hinnant8c6cbb22010-09-22 14:16:26774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19775 explicit __assoc_state_alloc(const _Alloc& __a)
776 : __alloc_(__a) {}
777};
778
Howard Hinnant99968442011-11-29 18:15:50779template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19780void
Howard Hinnant99968442011-11-29 18:15:50781__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19782{
783 if (this->__state_ & base::__constructed)
Howard Hinnanta4e87ab2013-08-08 18:38:55784 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
Eric Fiselier8492cd82015-02-05 23:01:40785 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
786 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45787 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40788 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19789 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45790 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19791}
792
Howard Hinnant99968442011-11-29 18:15:50793template <class _Rp, class _Alloc>
794class __assoc_state_alloc<_Rp&, _Alloc>
795 : public __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06796{
Howard Hinnant99968442011-11-29 18:15:50797 typedef __assoc_state<_Rp&> base;
Howard Hinnantf39daa82010-08-28 21:01:06798 _Alloc __alloc_;
799
Howard Hinnant1694d232011-05-28 14:41:13800 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06801public:
Howard Hinnant8c6cbb22010-09-22 14:16:26802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39daa82010-08-28 21:01:06803 explicit __assoc_state_alloc(const _Alloc& __a)
804 : __alloc_(__a) {}
805};
806
Howard Hinnant99968442011-11-29 18:15:50807template <class _Rp, class _Alloc>
Howard Hinnantf39daa82010-08-28 21:01:06808void
Howard Hinnant99968442011-11-29 18:15:50809__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06810{
Eric Fiselier8492cd82015-02-05 23:01:40811 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
812 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45813 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40814 _Al __a(__alloc_);
Howard Hinnantf39daa82010-08-28 21:01:06815 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45816 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantf39daa82010-08-28 21:01:06817}
818
Howard Hinnant47499b12010-08-27 20:10:19819template <class _Alloc>
820class __assoc_sub_state_alloc
821 : public __assoc_sub_state
822{
823 typedef __assoc_sub_state base;
824 _Alloc __alloc_;
825
Howard Hinnant1694d232011-05-28 14:41:13826 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19827public:
Howard Hinnant8c6cbb22010-09-22 14:16:26828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19829 explicit __assoc_sub_state_alloc(const _Alloc& __a)
830 : __alloc_(__a) {}
831};
832
833template <class _Alloc>
834void
Howard Hinnant1694d232011-05-28 14:41:13835__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19836{
Eric Fiselier8492cd82015-02-05 23:01:40837 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
838 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45839 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40840 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19841 this->~__assoc_sub_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45842 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19843}
844
Howard Hinnant99968442011-11-29 18:15:50845template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21846class __deferred_assoc_state
Howard Hinnant99968442011-11-29 18:15:50847 : public __assoc_state<_Rp>
Howard Hinnant54da3382010-08-30 18:46:21848{
Howard Hinnant99968442011-11-29 18:15:50849 typedef __assoc_state<_Rp> base;
Howard Hinnant54da3382010-08-30 18:46:21850
Howard Hinnant99968442011-11-29 18:15:50851 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21852
853public:
Howard Hinnant73d21a42010-09-04 23:28:19854#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50855 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21856#endif
857
858 virtual void __execute();
859};
860
Howard Hinnant73d21a42010-09-04 23:28:19861#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21862
Howard Hinnant99968442011-11-29 18:15:50863template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21864inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50865__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
866 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21867{
868 this->__set_deferred();
869}
870
Howard Hinnant73d21a42010-09-04 23:28:19871#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21872
Howard Hinnant99968442011-11-29 18:15:50873template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21874void
Howard Hinnant99968442011-11-29 18:15:50875__deferred_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21876{
877#ifndef _LIBCPP_NO_EXCEPTIONS
878 try
879 {
880#endif // _LIBCPP_NO_EXCEPTIONS
881 this->set_value(__func_());
882#ifndef _LIBCPP_NO_EXCEPTIONS
883 }
884 catch (...)
885 {
886 this->set_exception(current_exception());
887 }
888#endif // _LIBCPP_NO_EXCEPTIONS
889}
890
Howard Hinnant99968442011-11-29 18:15:50891template <class _Fp>
892class __deferred_assoc_state<void, _Fp>
Howard Hinnant54da3382010-08-30 18:46:21893 : public __assoc_sub_state
894{
895 typedef __assoc_sub_state base;
896
Howard Hinnant99968442011-11-29 18:15:50897 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21898
899public:
Howard Hinnant73d21a42010-09-04 23:28:19900#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50901 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21902#endif
903
904 virtual void __execute();
905};
906
Howard Hinnant73d21a42010-09-04 23:28:19907#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21908
Howard Hinnant99968442011-11-29 18:15:50909template <class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21910inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50911__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
912 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21913{
914 this->__set_deferred();
915}
916
Howard Hinnant73d21a42010-09-04 23:28:19917#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21918
Howard Hinnant99968442011-11-29 18:15:50919template <class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21920void
Howard Hinnant99968442011-11-29 18:15:50921__deferred_assoc_state<void, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21922{
923#ifndef _LIBCPP_NO_EXCEPTIONS
924 try
925 {
926#endif // _LIBCPP_NO_EXCEPTIONS
927 __func_();
928 this->set_value();
929#ifndef _LIBCPP_NO_EXCEPTIONS
930 }
931 catch (...)
932 {
933 this->set_exception(current_exception());
934 }
935#endif // _LIBCPP_NO_EXCEPTIONS
936}
937
Howard Hinnant99968442011-11-29 18:15:50938template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04939class __async_assoc_state
Howard Hinnant99968442011-11-29 18:15:50940 : public __assoc_state<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04941{
Howard Hinnant99968442011-11-29 18:15:50942 typedef __assoc_state<_Rp> base;
Howard Hinnant57cff292011-05-19 15:05:04943
Howard Hinnant99968442011-11-29 18:15:50944 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04945
Howard Hinnant1694d232011-05-28 14:41:13946 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04947public:
948#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50949 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04950#endif
951
952 virtual void __execute();
953};
954
955#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
956
Howard Hinnant99968442011-11-29 18:15:50957template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04958inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50959__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
960 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:04961{
962}
963
964#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
965
Howard Hinnant99968442011-11-29 18:15:50966template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04967void
Howard Hinnant99968442011-11-29 18:15:50968__async_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:04969{
970#ifndef _LIBCPP_NO_EXCEPTIONS
971 try
972 {
973#endif // _LIBCPP_NO_EXCEPTIONS
974 this->set_value(__func_());
975#ifndef _LIBCPP_NO_EXCEPTIONS
976 }
977 catch (...)
978 {
979 this->set_exception(current_exception());
980 }
981#endif // _LIBCPP_NO_EXCEPTIONS
982}
983
Howard Hinnant99968442011-11-29 18:15:50984template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04985void
Howard Hinnant99968442011-11-29 18:15:50986__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04987{
988 this->wait();
989 base::__on_zero_shared();
990}
991
Howard Hinnant99968442011-11-29 18:15:50992template <class _Fp>
993class __async_assoc_state<void, _Fp>
Howard Hinnant57cff292011-05-19 15:05:04994 : public __assoc_sub_state
995{
996 typedef __assoc_sub_state base;
997
Howard Hinnant99968442011-11-29 18:15:50998 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04999
Howard Hinnant1694d232011-05-28 14:41:131000 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:041001public:
1002#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501003 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:041004#endif
1005
1006 virtual void __execute();
1007};
1008
1009#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1010
Howard Hinnant99968442011-11-29 18:15:501011template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041012inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501013__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1014 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:041015{
1016}
1017
1018#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1019
Howard Hinnant99968442011-11-29 18:15:501020template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041021void
Howard Hinnant99968442011-11-29 18:15:501022__async_assoc_state<void, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:041023{
1024#ifndef _LIBCPP_NO_EXCEPTIONS
1025 try
1026 {
1027#endif // _LIBCPP_NO_EXCEPTIONS
1028 __func_();
1029 this->set_value();
1030#ifndef _LIBCPP_NO_EXCEPTIONS
1031 }
1032 catch (...)
1033 {
1034 this->set_exception(current_exception());
1035 }
1036#endif // _LIBCPP_NO_EXCEPTIONS
1037}
1038
Howard Hinnant99968442011-11-29 18:15:501039template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041040void
Howard Hinnant99968442011-11-29 18:15:501041__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:041042{
1043 this->wait();
1044 base::__on_zero_shared();
1045}
1046
Howard Hinnant0f678bd2013-08-12 18:38:341047template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY promise;
1048template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY shared_future;
Howard Hinnant47499b12010-08-27 20:10:191049
1050// future
1051
Howard Hinnant0f678bd2013-08-12 18:38:341052template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY future;
Howard Hinnant54da3382010-08-30 18:46:211053
Howard Hinnant99968442011-11-29 18:15:501054template <class _Rp, class _Fp>
1055future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:191056#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501057__make_deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211058#else
Howard Hinnant99968442011-11-29 18:15:501059__make_deferred_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211060#endif
1061
Howard Hinnant99968442011-11-29 18:15:501062template <class _Rp, class _Fp>
1063future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:041064#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501065__make_async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:041066#else
Howard Hinnant99968442011-11-29 18:15:501067__make_async_assoc_state(_Fp __f);
Howard Hinnant57cff292011-05-19 15:05:041068#endif
1069
Howard Hinnant99968442011-11-29 18:15:501070template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341071class _LIBCPP_TYPE_VIS_ONLY future
Howard Hinnant47499b12010-08-27 20:10:191072{
Howard Hinnant99968442011-11-29 18:15:501073 __assoc_state<_Rp>* __state_;
Howard Hinnant47499b12010-08-27 20:10:191074
Howard Hinnant99968442011-11-29 18:15:501075 explicit future(__assoc_state<_Rp>* __state);
Howard Hinnant47499b12010-08-27 20:10:191076
1077 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251078 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211079
Howard Hinnant73d21a42010-09-04 23:28:191080#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501081 template <class _R1, class _Fp>
1082 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1083 template <class _R1, class _Fp>
1084 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211085#else
Howard Hinnant99968442011-11-29 18:15:501086 template <class _R1, class _Fp>
1087 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1088 template <class _R1, class _Fp>
1089 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211090#endif
1091
Howard Hinnant47499b12010-08-27 20:10:191092public:
Howard Hinnant8c6cbb22010-09-22 14:16:261093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551094 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191095#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551097 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191098 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1099 future(const future&) = delete;
1100 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551102 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191103 {
1104 future(std::move(__rhs)).swap(*this);
1105 return *this;
1106 }
Howard Hinnant73d21a42010-09-04 23:28:191107#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191108private:
1109 future(const future&);
1110 future& operator=(const future&);
1111public:
Howard Hinnant73d21a42010-09-04 23:28:191112#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191113 ~future();
Howard Hinnant99968442011-11-29 18:15:501114 shared_future<_Rp> share();
Howard Hinnant47499b12010-08-27 20:10:191115
1116 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:501117 _Rp get();
Howard Hinnant47499b12010-08-27 20:10:191118
Howard Hinnant8c6cbb22010-09-22 14:16:261119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551120 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191121
1122 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551124 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191125
Howard Hinnant8c6cbb22010-09-22 14:16:261126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191127 void wait() const {__state_->wait();}
1128 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191130 future_status
1131 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1132 {return __state_->wait_for(__rel_time);}
1133 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191135 future_status
1136 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1137 {return __state_->wait_until(__abs_time);}
1138};
1139
Howard Hinnant99968442011-11-29 18:15:501140template <class _Rp>
1141future<_Rp>::future(__assoc_state<_Rp>* __state)
Howard Hinnant47499b12010-08-27 20:10:191142 : __state_(__state)
1143{
Howard Hinnant47499b12010-08-27 20:10:191144 if (__state_->__has_future_attached())
Marshall Clowa1899742015-09-03 15:11:321145 __throw_future_error<future_errc::future_already_retrieved>();
Howard Hinnant47499b12010-08-27 20:10:191146 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:211147 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:191148}
1149
Howard Hinnant54da3382010-08-30 18:46:211150struct __release_shared_count
1151{
1152 void operator()(__shared_count* p) {p->__release_shared();}
1153};
1154
Howard Hinnant99968442011-11-29 18:15:501155template <class _Rp>
1156future<_Rp>::~future()
Howard Hinnant47499b12010-08-27 20:10:191157{
1158 if (__state_)
1159 __state_->__release_shared();
1160}
1161
Howard Hinnant99968442011-11-29 18:15:501162template <class _Rp>
1163_Rp
1164future<_Rp>::get()
Howard Hinnant47499b12010-08-27 20:10:191165{
Howard Hinnant54da3382010-08-30 18:46:211166 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:501167 __assoc_state<_Rp>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:191168 __state_ = nullptr;
1169 return __s->move();
1170}
1171
Howard Hinnant99968442011-11-29 18:15:501172template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341173class _LIBCPP_TYPE_VIS_ONLY future<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:191174{
Howard Hinnant99968442011-11-29 18:15:501175 __assoc_state<_Rp&>* __state_;
Howard Hinnant47499b12010-08-27 20:10:191176
Howard Hinnant99968442011-11-29 18:15:501177 explicit future(__assoc_state<_Rp&>* __state);
Howard Hinnant47499b12010-08-27 20:10:191178
1179 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251180 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211181
Howard Hinnant73d21a42010-09-04 23:28:191182#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501183 template <class _R1, class _Fp>
1184 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1185 template <class _R1, class _Fp>
1186 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211187#else
Howard Hinnant99968442011-11-29 18:15:501188 template <class _R1, class _Fp>
1189 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1190 template <class _R1, class _Fp>
1191 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211192#endif
1193
Howard Hinnant47499b12010-08-27 20:10:191194public:
Howard Hinnant8c6cbb22010-09-22 14:16:261195 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551196 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191197#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551199 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191200 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1201 future(const future&) = delete;
1202 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551204 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191205 {
1206 future(std::move(__rhs)).swap(*this);
1207 return *this;
1208 }
Howard Hinnant73d21a42010-09-04 23:28:191209#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191210private:
1211 future(const future&);
1212 future& operator=(const future&);
1213public:
Howard Hinnant73d21a42010-09-04 23:28:191214#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191215 ~future();
Howard Hinnant99968442011-11-29 18:15:501216 shared_future<_Rp&> share();
Howard Hinnant47499b12010-08-27 20:10:191217
1218 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:501219 _Rp& get();
Howard Hinnant47499b12010-08-27 20:10:191220
Howard Hinnant8c6cbb22010-09-22 14:16:261221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551222 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191223
1224 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551226 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191227
Howard Hinnant8c6cbb22010-09-22 14:16:261228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191229 void wait() const {__state_->wait();}
1230 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191232 future_status
1233 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1234 {return __state_->wait_for(__rel_time);}
1235 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191237 future_status
1238 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1239 {return __state_->wait_until(__abs_time);}
1240};
1241
Howard Hinnant99968442011-11-29 18:15:501242template <class _Rp>
1243future<_Rp&>::future(__assoc_state<_Rp&>* __state)
Howard Hinnant47499b12010-08-27 20:10:191244 : __state_(__state)
1245{
Howard Hinnant47499b12010-08-27 20:10:191246 if (__state_->__has_future_attached())
Marshall Clowa1899742015-09-03 15:11:321247 __throw_future_error<future_errc::future_already_retrieved>();
Howard Hinnant47499b12010-08-27 20:10:191248 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:211249 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:191250}
1251
Howard Hinnant99968442011-11-29 18:15:501252template <class _Rp>
1253future<_Rp&>::~future()
Howard Hinnant47499b12010-08-27 20:10:191254{
1255 if (__state_)
1256 __state_->__release_shared();
1257}
1258
Howard Hinnant99968442011-11-29 18:15:501259template <class _Rp>
1260_Rp&
1261future<_Rp&>::get()
Howard Hinnant47499b12010-08-27 20:10:191262{
Howard Hinnant54da3382010-08-30 18:46:211263 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:501264 __assoc_state<_Rp&>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:191265 __state_ = nullptr;
1266 return __s->copy();
1267}
1268
1269template <>
Howard Hinnant83eade62013-03-06 23:30:191270class _LIBCPP_TYPE_VIS future<void>
Howard Hinnant47499b12010-08-27 20:10:191271{
1272 __assoc_sub_state* __state_;
1273
1274 explicit future(__assoc_sub_state* __state);
1275
1276 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251277 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211278
Howard Hinnant73d21a42010-09-04 23:28:191279#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501280 template <class _R1, class _Fp>
1281 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1282 template <class _R1, class _Fp>
1283 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211284#else
Howard Hinnant99968442011-11-29 18:15:501285 template <class _R1, class _Fp>
1286 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1287 template <class _R1, class _Fp>
1288 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211289#endif
1290
Howard Hinnant47499b12010-08-27 20:10:191291public:
Howard Hinnant8c6cbb22010-09-22 14:16:261292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551293 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191294#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551296 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191297 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1298 future(const future&) = delete;
1299 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551301 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191302 {
1303 future(std::move(__rhs)).swap(*this);
1304 return *this;
1305 }
Howard Hinnant73d21a42010-09-04 23:28:191306#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191307private:
1308 future(const future&);
1309 future& operator=(const future&);
1310public:
Howard Hinnant73d21a42010-09-04 23:28:191311#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191312 ~future();
Howard Hinnant7de47902010-11-30 20:23:321313 shared_future<void> share();
Howard Hinnant47499b12010-08-27 20:10:191314
1315 // retrieving the value
1316 void get();
1317
Howard Hinnant8c6cbb22010-09-22 14:16:261318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551319 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191320
1321 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551323 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191324
Howard Hinnant8c6cbb22010-09-22 14:16:261325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191326 void wait() const {__state_->wait();}
1327 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191329 future_status
1330 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1331 {return __state_->wait_for(__rel_time);}
1332 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191334 future_status
1335 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1336 {return __state_->wait_until(__abs_time);}
1337};
1338
Howard Hinnant99968442011-11-29 18:15:501339template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:251340inline _LIBCPP_INLINE_VISIBILITY
1341void
Howard Hinnant8bf01dd2012-07-21 17:46:551342swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:251343{
1344 __x.swap(__y);
1345}
1346
Howard Hinnant47499b12010-08-27 20:10:191347// promise<R>
1348
Howard Hinnant2b1b2d42011-06-14 19:58:171349template <class _Callable> class packaged_task;
Howard Hinnant54da3382010-08-30 18:46:211350
Howard Hinnant99968442011-11-29 18:15:501351template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341352class _LIBCPP_TYPE_VIS_ONLY promise
Howard Hinnant47499b12010-08-27 20:10:191353{
Howard Hinnant99968442011-11-29 18:15:501354 __assoc_state<_Rp>* __state_;
Howard Hinnant54da3382010-08-30 18:46:211355
Howard Hinnant8c6cbb22010-09-22 14:16:261356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551357 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211358
1359 template <class> friend class packaged_task;
Howard Hinnant47499b12010-08-27 20:10:191360public:
1361 promise();
1362 template <class _Alloc>
1363 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant73d21a42010-09-04 23:28:191364#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551366 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191367 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1368 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191369#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191370private:
1371 promise(const promise& __rhs);
1372public:
Howard Hinnant73d21a42010-09-04 23:28:191373#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191374 ~promise();
1375
1376 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191377#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551379 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191380 {
1381 promise(std::move(__rhs)).swap(*this);
1382 return *this;
1383 }
1384 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191385#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191386private:
1387 promise& operator=(const promise& __rhs);
1388public:
Howard Hinnant73d21a42010-09-04 23:28:191389#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551391 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191392
1393 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:501394 future<_Rp> get_future();
Howard Hinnant47499b12010-08-27 20:10:191395
1396 // setting the result
Howard Hinnant99968442011-11-29 18:15:501397 void set_value(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:191398#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501399 void set_value(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:191400#endif
1401 void set_exception(exception_ptr __p);
1402
1403 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:501404 void set_value_at_thread_exit(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:191405#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501406 void set_value_at_thread_exit(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:191407#endif
1408 void set_exception_at_thread_exit(exception_ptr __p);
1409};
1410
Howard Hinnant99968442011-11-29 18:15:501411template <class _Rp>
1412promise<_Rp>::promise()
1413 : __state_(new __assoc_state<_Rp>)
Howard Hinnant47499b12010-08-27 20:10:191414{
1415}
1416
Howard Hinnant99968442011-11-29 18:15:501417template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191418template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:501419promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:191420{
Eric Fiselier4d2413c2014-10-23 06:24:451421 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1422 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191423 typedef __allocator_destructor<_A2> _D2;
1424 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451425 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1426 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1427 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191428}
1429
Howard Hinnant99968442011-11-29 18:15:501430template <class _Rp>
1431promise<_Rp>::~promise()
Howard Hinnant47499b12010-08-27 20:10:191432{
1433 if (__state_)
1434 {
1435 if (!__state_->__has_value() && __state_->use_count() > 1)
1436 __state_->set_exception(make_exception_ptr(
1437 future_error(make_error_code(future_errc::broken_promise))
1438 ));
1439 __state_->__release_shared();
1440 }
1441}
1442
Howard Hinnant99968442011-11-29 18:15:501443template <class _Rp>
1444future<_Rp>
1445promise<_Rp>::get_future()
Howard Hinnant47499b12010-08-27 20:10:191446{
Howard Hinnant47499b12010-08-27 20:10:191447 if (__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:321448 __throw_future_error<future_errc::no_state>();
Howard Hinnant99968442011-11-29 18:15:501449 return future<_Rp>(__state_);
Howard Hinnant47499b12010-08-27 20:10:191450}
1451
Howard Hinnant99968442011-11-29 18:15:501452template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191453void
Howard Hinnant99968442011-11-29 18:15:501454promise<_Rp>::set_value(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191455{
Howard Hinnant47499b12010-08-27 20:10:191456 if (__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:321457 __throw_future_error<future_errc::no_state>();
Howard Hinnant47499b12010-08-27 20:10:191458 __state_->set_value(__r);
1459}
1460
Howard Hinnant73d21a42010-09-04 23:28:191461#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191462
Howard Hinnant99968442011-11-29 18:15:501463template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191464void
Howard Hinnant99968442011-11-29 18:15:501465promise<_Rp>::set_value(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:191466{
Howard Hinnant47499b12010-08-27 20:10:191467 if (__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:321468 __throw_future_error<future_errc::no_state>();
Howard Hinnant0949eed2011-06-30 21:18:191469 __state_->set_value(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:191470}
1471
Howard Hinnant73d21a42010-09-04 23:28:191472#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191473
Howard Hinnant99968442011-11-29 18:15:501474template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191475void
Howard Hinnant99968442011-11-29 18:15:501476promise<_Rp>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191477{
Howard Hinnant47499b12010-08-27 20:10:191478 if (__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:321479 __throw_future_error<future_errc::no_state>();
Howard Hinnant47499b12010-08-27 20:10:191480 __state_->set_exception(__p);
1481}
1482
Howard Hinnant99968442011-11-29 18:15:501483template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191484void
Howard Hinnant99968442011-11-29 18:15:501485promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191486{
Howard Hinnant47499b12010-08-27 20:10:191487 if (__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:321488 __throw_future_error<future_errc::no_state>();
Howard Hinnant47499b12010-08-27 20:10:191489 __state_->set_value_at_thread_exit(__r);
1490}
1491
Howard Hinnant73d21a42010-09-04 23:28:191492#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191493
Howard Hinnant99968442011-11-29 18:15:501494template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191495void
Howard Hinnant99968442011-11-29 18:15:501496promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:191497{
Howard Hinnant47499b12010-08-27 20:10:191498 if (__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:321499 __throw_future_error<future_errc::no_state>();
Howard Hinnant0949eed2011-06-30 21:18:191500 __state_->set_value_at_thread_exit(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:191501}
1502
Howard Hinnant73d21a42010-09-04 23:28:191503#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191504
Howard Hinnant99968442011-11-29 18:15:501505template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191506void
Howard Hinnant99968442011-11-29 18:15:501507promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191508{
Howard Hinnant47499b12010-08-27 20:10:191509 if (__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:321510 __throw_future_error<future_errc::no_state>();
Howard Hinnant47499b12010-08-27 20:10:191511 __state_->set_exception_at_thread_exit(__p);
1512}
1513
1514// promise<R&>
1515
Howard Hinnant99968442011-11-29 18:15:501516template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341517class _LIBCPP_TYPE_VIS_ONLY promise<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:191518{
Howard Hinnant99968442011-11-29 18:15:501519 __assoc_state<_Rp&>* __state_;
Howard Hinnant54da3382010-08-30 18:46:211520
Howard Hinnant8c6cbb22010-09-22 14:16:261521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551522 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211523
1524 template <class> friend class packaged_task;
1525
Howard Hinnant47499b12010-08-27 20:10:191526public:
1527 promise();
1528 template <class _Allocator>
1529 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:191530#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551532 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191533 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1534 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191535#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191536private:
1537 promise(const promise& __rhs);
1538public:
Howard Hinnant73d21a42010-09-04 23:28:191539#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191540 ~promise();
1541
1542 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191543#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551545 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191546 {
1547 promise(std::move(__rhs)).swap(*this);
1548 return *this;
1549 }
1550 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191551#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191552private:
1553 promise& operator=(const promise& __rhs);
1554public:
Howard Hinnant73d21a42010-09-04 23:28:191555#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551557 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191558
1559 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:501560 future<_Rp&> get_future();
Howard Hinnant47499b12010-08-27 20:10:191561
1562 // setting the result
Howard Hinnant99968442011-11-29 18:15:501563 void set_value(_Rp& __r);
Howard Hinnant47499b12010-08-27 20:10:191564 void set_exception(exception_ptr __p);
1565
1566 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:501567 void set_value_at_thread_exit(_Rp&);
Howard Hinnant47499b12010-08-27 20:10:191568 void set_exception_at_thread_exit(exception_ptr __p);
1569};
1570
Howard Hinnant99968442011-11-29 18:15:501571template <class _Rp>
1572promise<_Rp&>::promise()
1573 : __state_(new __assoc_state<_Rp&>)
Howard Hinnant47499b12010-08-27 20:10:191574{
1575}
1576
Howard Hinnant99968442011-11-29 18:15:501577template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191578template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:501579promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:191580{
Eric Fiselier4d2413c2014-10-23 06:24:451581 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1582 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191583 typedef __allocator_destructor<_A2> _D2;
1584 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451585 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1586 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1587 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191588}
1589
Howard Hinnant99968442011-11-29 18:15:501590template <class _Rp>
1591promise<_Rp&>::~promise()
Howard Hinnant47499b12010-08-27 20:10:191592{
1593 if (__state_)
1594 {
1595 if (!__state_->__has_value() && __state_->use_count() > 1)
1596 __state_->set_exception(make_exception_ptr(
1597 future_error(make_error_code(future_errc::broken_promise))
1598 ));
1599 __state_->__release_shared();
1600 }
1601}
1602
Howard Hinnant99968442011-11-29 18:15:501603template <class _Rp>
1604future<_Rp&>
1605promise<_Rp&>::get_future()
Howard Hinnant47499b12010-08-27 20:10:191606{
Howard Hinnant47499b12010-08-27 20:10:191607 if (__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:321608 __throw_future_error<future_errc::no_state>();
Howard Hinnant99968442011-11-29 18:15:501609 return future<_Rp&>(__state_);
Howard Hinnant47499b12010-08-27 20:10:191610}
1611
Howard Hinnant99968442011-11-29 18:15:501612template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191613void
Howard Hinnant99968442011-11-29 18:15:501614promise<_Rp&>::set_value(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191615{
Howard Hinnant47499b12010-08-27 20:10:191616 if (__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:321617 __throw_future_error<future_errc::no_state>();
Howard Hinnant47499b12010-08-27 20:10:191618 __state_->set_value(__r);
1619}
1620
Howard Hinnant99968442011-11-29 18:15:501621template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191622void
Howard Hinnant99968442011-11-29 18:15:501623promise<_Rp&>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191624{
Howard Hinnant47499b12010-08-27 20:10:191625 if (__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:321626 __throw_future_error<future_errc::no_state>();
Howard Hinnant47499b12010-08-27 20:10:191627 __state_->set_exception(__p);
1628}
1629
Howard Hinnant99968442011-11-29 18:15:501630template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191631void
Howard Hinnant99968442011-11-29 18:15:501632promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191633{
Howard Hinnant47499b12010-08-27 20:10:191634 if (__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:321635 __throw_future_error<future_errc::no_state>();
Howard Hinnant47499b12010-08-27 20:10:191636 __state_->set_value_at_thread_exit(__r);
1637}
1638
Howard Hinnant99968442011-11-29 18:15:501639template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191640void
Howard Hinnant99968442011-11-29 18:15:501641promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191642{
Howard Hinnant47499b12010-08-27 20:10:191643 if (__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:321644 __throw_future_error<future_errc::no_state>();
Howard Hinnant47499b12010-08-27 20:10:191645 __state_->set_exception_at_thread_exit(__p);
1646}
1647
1648// promise<void>
1649
1650template <>
Howard Hinnant83eade62013-03-06 23:30:191651class _LIBCPP_TYPE_VIS promise<void>
Howard Hinnant47499b12010-08-27 20:10:191652{
1653 __assoc_sub_state* __state_;
Howard Hinnant54da3382010-08-30 18:46:211654
Howard Hinnant8c6cbb22010-09-22 14:16:261655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551656 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211657
1658 template <class> friend class packaged_task;
1659
Howard Hinnant47499b12010-08-27 20:10:191660public:
1661 promise();
1662 template <class _Allocator>
1663 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:191664#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551666 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191667 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1668 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191669#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191670private:
1671 promise(const promise& __rhs);
1672public:
Howard Hinnant73d21a42010-09-04 23:28:191673#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191674 ~promise();
1675
1676 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191677#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551679 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191680 {
1681 promise(std::move(__rhs)).swap(*this);
1682 return *this;
1683 }
1684 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191685#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191686private:
1687 promise& operator=(const promise& __rhs);
1688public:
Howard Hinnant73d21a42010-09-04 23:28:191689#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261690 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551691 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191692
1693 // retrieving the result
1694 future<void> get_future();
1695
1696 // setting the result
1697 void set_value();
1698 void set_exception(exception_ptr __p);
1699
1700 // setting the result with deferred notification
1701 void set_value_at_thread_exit();
1702 void set_exception_at_thread_exit(exception_ptr __p);
1703};
1704
1705template <class _Alloc>
1706promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1707{
Eric Fiselier4d2413c2014-10-23 06:24:451708 typedef __assoc_sub_state_alloc<_Alloc> _State;
1709 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191710 typedef __allocator_destructor<_A2> _D2;
1711 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451712 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1713 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1714 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191715}
1716
Howard Hinnant99968442011-11-29 18:15:501717template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191718inline _LIBCPP_INLINE_VISIBILITY
1719void
Howard Hinnant8bf01dd2012-07-21 17:46:551720swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191721{
1722 __x.swap(__y);
1723}
1724
Howard Hinnant99968442011-11-29 18:15:501725template <class _Rp, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:341726 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<promise<_Rp>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:261727 : public true_type {};
Howard Hinnant47499b12010-08-27 20:10:191728
Howard Hinnant54da3382010-08-30 18:46:211729#ifndef _LIBCPP_HAS_NO_VARIADICS
1730
1731// packaged_task
1732
1733template<class _Fp> class __packaged_task_base;
1734
Howard Hinnant99968442011-11-29 18:15:501735template<class _Rp, class ..._ArgTypes>
1736class __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211737{
1738 __packaged_task_base(const __packaged_task_base&);
1739 __packaged_task_base& operator=(const __packaged_task_base&);
1740public:
Howard Hinnant8c6cbb22010-09-22 14:16:261741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:211742 __packaged_task_base() {}
Howard Hinnant8c6cbb22010-09-22 14:16:261743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:211744 virtual ~__packaged_task_base() {}
Howard Hinnant8bf01dd2012-07-21 17:46:551745 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
Howard Hinnant54da3382010-08-30 18:46:211746 virtual void destroy() = 0;
1747 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:501748 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant54da3382010-08-30 18:46:211749};
1750
1751template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1752
Howard Hinnant99968442011-11-29 18:15:501753template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1754class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1755 : public __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211756{
Howard Hinnant99968442011-11-29 18:15:501757 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnant54da3382010-08-30 18:46:211758public:
Howard Hinnant8c6cbb22010-09-22 14:16:261759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501760 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501762 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501764 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
Howard Hinnant54da3382010-08-30 18:46:211765 : __f_(__f, __a) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501767 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
Howard Hinnant0949eed2011-06-30 21:18:191768 : __f_(_VSTD::move(__f), __a) {}
Howard Hinnant8bf01dd2012-07-21 17:46:551769 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211770 virtual void destroy();
1771 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:501772 virtual _Rp operator()(_ArgTypes&& ... __args);
Howard Hinnant54da3382010-08-30 18:46:211773};
1774
Howard Hinnant99968442011-11-29 18:15:501775template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211776void
Howard Hinnant99968442011-11-29 18:15:501777__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
Howard Hinnant8bf01dd2012-07-21 17:46:551778 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211779{
Howard Hinnant0949eed2011-06-30 21:18:191780 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
Howard Hinnant54da3382010-08-30 18:46:211781}
1782
Howard Hinnant99968442011-11-29 18:15:501783template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211784void
Howard Hinnant99968442011-11-29 18:15:501785__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
Howard Hinnant54da3382010-08-30 18:46:211786{
Howard Hinnant99968442011-11-29 18:15:501787 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnant54da3382010-08-30 18:46:211788}
1789
Howard Hinnant99968442011-11-29 18:15:501790template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211791void
Howard Hinnant99968442011-11-29 18:15:501792__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
Howard Hinnant54da3382010-08-30 18:46:211793{
Eric Fiselier4d2413c2014-10-23 06:24:451794 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1795 typedef allocator_traits<_Ap> _ATraits;
1796 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Howard Hinnant99968442011-11-29 18:15:501797 _Ap __a(__f_.second());
1798 __f_.~__compressed_pair<_Fp, _Alloc>();
Eric Fiselier4d2413c2014-10-23 06:24:451799 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant54da3382010-08-30 18:46:211800}
1801
Howard Hinnant99968442011-11-29 18:15:501802template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1803_Rp
1804__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnant54da3382010-08-30 18:46:211805{
Howard Hinnant0949eed2011-06-30 21:18:191806 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:211807}
1808
Howard Hinnant2b1b2d42011-06-14 19:58:171809template <class _Callable> class __packaged_task_function;
Howard Hinnant54da3382010-08-30 18:46:211810
Howard Hinnant99968442011-11-29 18:15:501811template<class _Rp, class ..._ArgTypes>
1812class __packaged_task_function<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211813{
Howard Hinnant99968442011-11-29 18:15:501814 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
Howard Hinnant78f0de22013-01-21 17:26:551815 typename aligned_storage<3*sizeof(void*)>::type __buf_;
Howard Hinnant54da3382010-08-30 18:46:211816 __base* __f_;
1817
1818public:
Howard Hinnant99968442011-11-29 18:15:501819 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:211820
1821 // construct/copy/destroy:
Howard Hinnant8c6cbb22010-09-22 14:16:261822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551823 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
Howard Hinnant99968442011-11-29 18:15:501824 template<class _Fp>
1825 __packaged_task_function(_Fp&& __f);
1826 template<class _Fp, class _Alloc>
1827 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211828
Howard Hinnant8bf01dd2012-07-21 17:46:551829 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1830 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211831
1832 __packaged_task_function(const __packaged_task_function&) = delete;
1833 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1834
1835 ~__packaged_task_function();
1836
Howard Hinnant8bf01dd2012-07-21 17:46:551837 void swap(__packaged_task_function&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211838
Howard Hinnant99968442011-11-29 18:15:501839 _Rp operator()(_ArgTypes...) const;
Howard Hinnant54da3382010-08-30 18:46:211840};
1841
Howard Hinnant99968442011-11-29 18:15:501842template<class _Rp, class ..._ArgTypes>
Howard Hinnant8bf01dd2012-07-21 17:46:551843__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211844{
1845 if (__f.__f_ == nullptr)
1846 __f_ = nullptr;
1847 else if (__f.__f_ == (__base*)&__f.__buf_)
1848 {
1849 __f_ = (__base*)&__buf_;
1850 __f.__f_->__move_to(__f_);
1851 }
1852 else
1853 {
1854 __f_ = __f.__f_;
1855 __f.__f_ = nullptr;
1856 }
1857}
1858
Howard Hinnant99968442011-11-29 18:15:501859template<class _Rp, class ..._ArgTypes>
1860template <class _Fp>
1861__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:211862 : __f_(nullptr)
1863{
Marshall Clowf1264e72014-04-07 13:32:261864 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:501865 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:211866 if (sizeof(_FF) <= sizeof(__buf_))
1867 {
1868 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:501869 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:211870 }
1871 else
1872 {
Howard Hinnant99968442011-11-29 18:15:501873 typedef allocator<_FF> _Ap;
1874 _Ap __a;
1875 typedef __allocator_destructor<_Ap> _Dp;
1876 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1877 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
Howard Hinnant54da3382010-08-30 18:46:211878 __f_ = __hold.release();
1879 }
1880}
1881
Howard Hinnant99968442011-11-29 18:15:501882template<class _Rp, class ..._ArgTypes>
1883template <class _Fp, class _Alloc>
1884__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1885 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:211886 : __f_(nullptr)
1887{
Marshall Clowf1264e72014-04-07 13:32:261888 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:501889 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:211890 if (sizeof(_FF) <= sizeof(__buf_))
1891 {
1892 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:501893 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:211894 }
1895 else
1896 {
Eric Fiselier4d2413c2014-10-23 06:24:451897 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:501898 _Ap __a(__a0);
1899 typedef __allocator_destructor<_Ap> _Dp;
1900 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier4d2413c2014-10-23 06:24:451901 ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1902 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1903 __f_ = _VSTD::addressof(*__hold.release());
Howard Hinnant54da3382010-08-30 18:46:211904 }
1905}
1906
Howard Hinnant99968442011-11-29 18:15:501907template<class _Rp, class ..._ArgTypes>
1908__packaged_task_function<_Rp(_ArgTypes...)>&
Howard Hinnant8bf01dd2012-07-21 17:46:551909__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211910{
1911 if (__f_ == (__base*)&__buf_)
1912 __f_->destroy();
1913 else if (__f_)
1914 __f_->destroy_deallocate();
1915 __f_ = nullptr;
1916 if (__f.__f_ == nullptr)
1917 __f_ = nullptr;
1918 else if (__f.__f_ == (__base*)&__f.__buf_)
1919 {
1920 __f_ = (__base*)&__buf_;
1921 __f.__f_->__move_to(__f_);
1922 }
1923 else
1924 {
1925 __f_ = __f.__f_;
1926 __f.__f_ = nullptr;
1927 }
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:451928 return *this;
Howard Hinnant54da3382010-08-30 18:46:211929}
1930
Howard Hinnant99968442011-11-29 18:15:501931template<class _Rp, class ..._ArgTypes>
1932__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
Howard Hinnant54da3382010-08-30 18:46:211933{
1934 if (__f_ == (__base*)&__buf_)
1935 __f_->destroy();
1936 else if (__f_)
1937 __f_->destroy_deallocate();
1938}
1939
Howard Hinnant99968442011-11-29 18:15:501940template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211941void
Howard Hinnant8bf01dd2012-07-21 17:46:551942__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211943{
1944 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1945 {
1946 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1947 __base* __t = (__base*)&__tempbuf;
1948 __f_->__move_to(__t);
1949 __f_->destroy();
1950 __f_ = nullptr;
1951 __f.__f_->__move_to((__base*)&__buf_);
1952 __f.__f_->destroy();
1953 __f.__f_ = nullptr;
1954 __f_ = (__base*)&__buf_;
1955 __t->__move_to((__base*)&__f.__buf_);
1956 __t->destroy();
1957 __f.__f_ = (__base*)&__f.__buf_;
1958 }
1959 else if (__f_ == (__base*)&__buf_)
1960 {
1961 __f_->__move_to((__base*)&__f.__buf_);
1962 __f_->destroy();
1963 __f_ = __f.__f_;
1964 __f.__f_ = (__base*)&__f.__buf_;
1965 }
1966 else if (__f.__f_ == (__base*)&__f.__buf_)
1967 {
1968 __f.__f_->__move_to((__base*)&__buf_);
1969 __f.__f_->destroy();
1970 __f.__f_ = __f_;
1971 __f_ = (__base*)&__buf_;
1972 }
1973 else
Howard Hinnant0949eed2011-06-30 21:18:191974 _VSTD::swap(__f_, __f.__f_);
Howard Hinnant54da3382010-08-30 18:46:211975}
1976
Howard Hinnant99968442011-11-29 18:15:501977template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211978inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501979_Rp
1980__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnant54da3382010-08-30 18:46:211981{
Howard Hinnant0949eed2011-06-30 21:18:191982 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:211983}
1984
Howard Hinnant99968442011-11-29 18:15:501985template<class _Rp, class ..._ArgTypes>
Howard Hinnant0f678bd2013-08-12 18:38:341986class _LIBCPP_TYPE_VIS_ONLY packaged_task<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211987{
1988public:
Howard Hinnant99968442011-11-29 18:15:501989 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:211990
1991private:
1992 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1993 promise<result_type> __p_;
1994
1995public:
1996 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:261997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551998 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:171999 template <class _Fp,
2000 class = typename enable_if
2001 <
2002 !is_same<
2003 typename decay<_Fp>::type,
2004 packaged_task
2005 >::value
2006 >::type
2007 >
Howard Hinnant8c6cbb22010-09-22 14:16:262008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502009 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:172010 template <class _Fp, class _Allocator,
2011 class = typename enable_if
2012 <
2013 !is_same<
2014 typename decay<_Fp>::type,
2015 packaged_task
2016 >::value
2017 >::type
2018 >
Howard Hinnant8c6cbb22010-09-22 14:16:262019 _LIBCPP_INLINE_VISIBILITY
Marshall Clow07546f32015-06-30 14:16:492020 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:502021 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:212022 __p_(allocator_arg, __a) {}
2023 // ~packaged_task() = default;
2024
2025 // no copy
Howard Hinnant8131a012012-07-21 19:34:122026 packaged_task(const packaged_task&) = delete;
2027 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:212028
2029 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:262030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552031 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:192032 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552034 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212035 {
Howard Hinnant0949eed2011-06-30 21:18:192036 __f_ = _VSTD::move(__other.__f_);
2037 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:212038 return *this;
2039 }
Howard Hinnant8c6cbb22010-09-22 14:16:262040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552041 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212042 {
2043 __f_.swap(__other.__f_);
2044 __p_.swap(__other.__p_);
2045 }
2046
Howard Hinnant8c6cbb22010-09-22 14:16:262047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552048 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:212049
2050 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:262051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:212052 future<result_type> get_future() {return __p_.get_future();}
2053
2054 // execution
2055 void operator()(_ArgTypes... __args);
2056 void make_ready_at_thread_exit(_ArgTypes... __args);
2057
2058 void reset();
2059};
2060
Howard Hinnant99968442011-11-29 18:15:502061template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212062void
Howard Hinnant99968442011-11-29 18:15:502063packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:212064{
Howard Hinnant54da3382010-08-30 18:46:212065 if (__p_.__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:322066 __throw_future_error<future_errc::no_state>();
Howard Hinnant54da3382010-08-30 18:46:212067 if (__p_.__state_->__has_value())
Marshall Clowa1899742015-09-03 15:11:322068 __throw_future_error<future_errc::promise_already_satisfied>();
2069#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212070 try
2071 {
2072#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192073 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:212074#ifndef _LIBCPP_NO_EXCEPTIONS
2075 }
2076 catch (...)
2077 {
2078 __p_.set_exception(current_exception());
2079 }
2080#endif // _LIBCPP_NO_EXCEPTIONS
2081}
2082
Howard Hinnant99968442011-11-29 18:15:502083template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212084void
Howard Hinnant99968442011-11-29 18:15:502085packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:212086{
Howard Hinnant54da3382010-08-30 18:46:212087 if (__p_.__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:322088 __throw_future_error<future_errc::no_state>();
Howard Hinnant54da3382010-08-30 18:46:212089 if (__p_.__state_->__has_value())
Marshall Clowa1899742015-09-03 15:11:322090 __throw_future_error<future_errc::promise_already_satisfied>();
2091#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212092 try
2093 {
2094#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192095 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:212096#ifndef _LIBCPP_NO_EXCEPTIONS
2097 }
2098 catch (...)
2099 {
2100 __p_.set_exception_at_thread_exit(current_exception());
2101 }
2102#endif // _LIBCPP_NO_EXCEPTIONS
2103}
2104
Howard Hinnant99968442011-11-29 18:15:502105template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212106void
Howard Hinnant99968442011-11-29 18:15:502107packaged_task<_Rp(_ArgTypes...)>::reset()
Howard Hinnant54da3382010-08-30 18:46:212108{
Howard Hinnant7de47902010-11-30 20:23:322109 if (!valid())
Marshall Clowa1899742015-09-03 15:11:322110 __throw_future_error<future_errc::no_state>();
Howard Hinnant54da3382010-08-30 18:46:212111 __p_ = promise<result_type>();
2112}
2113
2114template<class ..._ArgTypes>
Howard Hinnant0f678bd2013-08-12 18:38:342115class _LIBCPP_TYPE_VIS_ONLY packaged_task<void(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:212116{
2117public:
2118 typedef void result_type;
2119
2120private:
2121 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2122 promise<result_type> __p_;
2123
2124public:
2125 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:262126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552127 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:172128 template <class _Fp,
2129 class = typename enable_if
2130 <
2131 !is_same<
2132 typename decay<_Fp>::type,
2133 packaged_task
2134 >::value
2135 >::type
2136 >
Howard Hinnant8c6cbb22010-09-22 14:16:262137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502138 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:172139 template <class _Fp, class _Allocator,
2140 class = typename enable_if
2141 <
2142 !is_same<
2143 typename decay<_Fp>::type,
2144 packaged_task
2145 >::value
2146 >::type
2147 >
Howard Hinnant8c6cbb22010-09-22 14:16:262148 _LIBCPP_INLINE_VISIBILITY
Marshall Clow5706c372015-06-30 18:28:352149 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:502150 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:212151 __p_(allocator_arg, __a) {}
2152 // ~packaged_task() = default;
2153
2154 // no copy
Howard Hinnant8131a012012-07-21 19:34:122155 packaged_task(const packaged_task&) = delete;
2156 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:212157
2158 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:262159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552160 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:192161 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552163 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212164 {
Howard Hinnant0949eed2011-06-30 21:18:192165 __f_ = _VSTD::move(__other.__f_);
2166 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:212167 return *this;
2168 }
Howard Hinnant8c6cbb22010-09-22 14:16:262169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552170 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212171 {
2172 __f_.swap(__other.__f_);
2173 __p_.swap(__other.__p_);
2174 }
2175
Howard Hinnant8c6cbb22010-09-22 14:16:262176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552177 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:212178
2179 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:262180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:212181 future<result_type> get_future() {return __p_.get_future();}
2182
2183 // execution
2184 void operator()(_ArgTypes... __args);
2185 void make_ready_at_thread_exit(_ArgTypes... __args);
2186
2187 void reset();
2188};
2189
2190template<class ..._ArgTypes>
2191void
2192packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2193{
Howard Hinnant54da3382010-08-30 18:46:212194 if (__p_.__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:322195 __throw_future_error<future_errc::no_state>();
Howard Hinnant54da3382010-08-30 18:46:212196 if (__p_.__state_->__has_value())
Marshall Clowa1899742015-09-03 15:11:322197 __throw_future_error<future_errc::promise_already_satisfied>();
2198#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212199 try
2200 {
2201#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192202 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212203 __p_.set_value();
2204#ifndef _LIBCPP_NO_EXCEPTIONS
2205 }
2206 catch (...)
2207 {
2208 __p_.set_exception(current_exception());
2209 }
2210#endif // _LIBCPP_NO_EXCEPTIONS
2211}
2212
2213template<class ..._ArgTypes>
2214void
2215packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2216{
Howard Hinnant54da3382010-08-30 18:46:212217 if (__p_.__state_ == nullptr)
Marshall Clowa1899742015-09-03 15:11:322218 __throw_future_error<future_errc::no_state>();
Howard Hinnant54da3382010-08-30 18:46:212219 if (__p_.__state_->__has_value())
Marshall Clowa1899742015-09-03 15:11:322220 __throw_future_error<future_errc::promise_already_satisfied>();
2221#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212222 try
2223 {
2224#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192225 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212226 __p_.set_value_at_thread_exit();
2227#ifndef _LIBCPP_NO_EXCEPTIONS
2228 }
2229 catch (...)
2230 {
2231 __p_.set_exception_at_thread_exit(current_exception());
2232 }
2233#endif // _LIBCPP_NO_EXCEPTIONS
2234}
2235
2236template<class ..._ArgTypes>
2237void
2238packaged_task<void(_ArgTypes...)>::reset()
2239{
Howard Hinnant7de47902010-11-30 20:23:322240 if (!valid())
Marshall Clowa1899742015-09-03 15:11:322241 __throw_future_error<future_errc::no_state>();
Howard Hinnant54da3382010-08-30 18:46:212242 __p_ = promise<result_type>();
2243}
2244
2245template <class _Callable>
2246inline _LIBCPP_INLINE_VISIBILITY
2247void
Howard Hinnant8bf01dd2012-07-21 17:46:552248swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212249{
2250 __x.swap(__y);
2251}
2252
2253template <class _Callable, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:342254struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<packaged_task<_Callable>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:262255 : public true_type {};
Howard Hinnant54da3382010-08-30 18:46:212256
Howard Hinnant99968442011-11-29 18:15:502257template <class _Rp, class _Fp>
2258future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:192259#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:502260__make_deferred_assoc_state(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:212261#else
Howard Hinnant99968442011-11-29 18:15:502262__make_deferred_assoc_state(_Fp __f)
Howard Hinnant54da3382010-08-30 18:46:212263#endif
2264{
Howard Hinnant99968442011-11-29 18:15:502265 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2266 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2267 return future<_Rp>(__h.get());
Howard Hinnant54da3382010-08-30 18:46:212268}
2269
Howard Hinnant99968442011-11-29 18:15:502270template <class _Rp, class _Fp>
2271future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:042272#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:502273__make_async_assoc_state(_Fp&& __f)
Howard Hinnant57cff292011-05-19 15:05:042274#else
Howard Hinnant99968442011-11-29 18:15:502275__make_async_assoc_state(_Fp __f)
Howard Hinnant57cff292011-05-19 15:05:042276#endif
2277{
Howard Hinnant99968442011-11-29 18:15:502278 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2279 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2280 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2281 return future<_Rp>(__h.get());
Howard Hinnant57cff292011-05-19 15:05:042282}
2283
Howard Hinnant99968442011-11-29 18:15:502284template <class _Fp, class... _Args>
Howard Hinnant57cff292011-05-19 15:05:042285class __async_func
2286{
Howard Hinnant99968442011-11-29 18:15:502287 tuple<_Fp, _Args...> __f_;
Howard Hinnant57cff292011-05-19 15:05:042288
2289public:
Howard Hinnant99968442011-11-29 18:15:502290 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
Howard Hinnant57cff292011-05-19 15:05:042291
2292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502293 explicit __async_func(_Fp&& __f, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:192294 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
Howard Hinnant57cff292011-05-19 15:05:042295
2296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:192297 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnant57cff292011-05-19 15:05:042298
Howard Hinnant99968442011-11-29 18:15:502299 _Rp operator()()
Howard Hinnant57cff292011-05-19 15:05:042300 {
2301 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2302 return __execute(_Index());
2303 }
2304private:
2305 template <size_t ..._Indices>
Howard Hinnant99968442011-11-29 18:15:502306 _Rp
Howard Hinnant57cff292011-05-19 15:05:042307 __execute(__tuple_indices<_Indices...>)
2308 {
Howard Hinnant0949eed2011-06-30 21:18:192309 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
Howard Hinnant57cff292011-05-19 15:05:042310 }
2311};
2312
Marshall Clow3b3108e2013-11-03 22:06:532313inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
Marshall Clowad2a6002013-11-03 15:43:352314{ return (int(__policy) & int(__value)) != 0; }
2315
Howard Hinnant99968442011-11-29 18:15:502316template <class _Fp, class... _Args>
2317future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2318async(launch __policy, _Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:212319{
Howard Hinnant99968442011-11-29 18:15:502320 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2321 typedef typename _BF::_Rp _Rp;
Marshall Clowad2a6002013-11-03 15:43:352322
2323#ifndef _LIBCPP_NO_EXCEPTIONS
2324 try
2325 {
2326#endif
2327 if (__does_policy_contain(__policy, launch::async))
2328 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:192329 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:352330#ifndef _LIBCPP_NO_EXCEPTIONS
2331 }
2332 catch ( ... ) { if (__policy == launch::async) throw ; }
2333#endif
2334
2335 if (__does_policy_contain(__policy, launch::deferred))
2336 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:192337 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:352338 return future<_Rp>{};
Howard Hinnant54da3382010-08-30 18:46:212339}
2340
Howard Hinnant99968442011-11-29 18:15:502341template <class _Fp, class... _Args>
Howard Hinnant54da3382010-08-30 18:46:212342inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502343future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2344async(_Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:212345{
Howard Hinnant99968442011-11-29 18:15:502346 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:192347 _VSTD::forward<_Args>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212348}
2349
2350#endif // _LIBCPP_HAS_NO_VARIADICS
2351
Howard Hinnante6e4d012010-09-03 21:46:372352// shared_future
2353
Howard Hinnant99968442011-11-29 18:15:502354template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:342355class _LIBCPP_TYPE_VIS_ONLY shared_future
Howard Hinnant99be8232010-09-03 18:39:252356{
Howard Hinnant99968442011-11-29 18:15:502357 __assoc_state<_Rp>* __state_;
Howard Hinnant99be8232010-09-03 18:39:252358
2359public:
Howard Hinnant8c6cbb22010-09-22 14:16:262360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552361 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252363 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2364 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192365#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552367 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252368 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552370 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252371 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192372#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252373 ~shared_future();
2374 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192375#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552377 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252378 {
2379 shared_future(std::move(__rhs)).swap(*this);
2380 return *this;
2381 }
Howard Hinnant73d21a42010-09-04 23:28:192382#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252383
2384 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502386 const _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:252387
Howard Hinnant8c6cbb22010-09-22 14:16:262388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552389 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252390
2391 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552393 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252394
Howard Hinnant8c6cbb22010-09-22 14:16:262395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252396 void wait() const {__state_->wait();}
2397 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252399 future_status
2400 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2401 {return __state_->wait_for(__rel_time);}
2402 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252404 future_status
2405 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2406 {return __state_->wait_until(__abs_time);}
2407};
2408
Howard Hinnant99968442011-11-29 18:15:502409template <class _Rp>
2410shared_future<_Rp>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:252411{
2412 if (__state_)
2413 __state_->__release_shared();
2414}
2415
Howard Hinnant99968442011-11-29 18:15:502416template <class _Rp>
2417shared_future<_Rp>&
2418shared_future<_Rp>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:252419{
2420 if (__rhs.__state_)
2421 __rhs.__state_->__add_shared();
2422 if (__state_)
2423 __state_->__release_shared();
2424 __state_ = __rhs.__state_;
2425 return *this;
2426}
2427
Howard Hinnant99968442011-11-29 18:15:502428template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:342429class _LIBCPP_TYPE_VIS_ONLY shared_future<_Rp&>
Howard Hinnant99be8232010-09-03 18:39:252430{
Howard Hinnant99968442011-11-29 18:15:502431 __assoc_state<_Rp&>* __state_;
Howard Hinnant99be8232010-09-03 18:39:252432
2433public:
Howard Hinnant8c6cbb22010-09-22 14:16:262434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552435 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252437 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2438 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192439#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552441 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252442 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552444 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252445 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192446#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252447 ~shared_future();
2448 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192449#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552451 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252452 {
2453 shared_future(std::move(__rhs)).swap(*this);
2454 return *this;
2455 }
Howard Hinnant73d21a42010-09-04 23:28:192456#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252457
2458 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502460 _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:252461
Howard Hinnant8c6cbb22010-09-22 14:16:262462 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552463 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252464
2465 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552467 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252468
Howard Hinnant8c6cbb22010-09-22 14:16:262469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252470 void wait() const {__state_->wait();}
2471 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252473 future_status
2474 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2475 {return __state_->wait_for(__rel_time);}
2476 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252478 future_status
2479 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2480 {return __state_->wait_until(__abs_time);}
2481};
2482
Howard Hinnant99968442011-11-29 18:15:502483template <class _Rp>
2484shared_future<_Rp&>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:252485{
2486 if (__state_)
2487 __state_->__release_shared();
2488}
2489
Howard Hinnant99968442011-11-29 18:15:502490template <class _Rp>
2491shared_future<_Rp&>&
2492shared_future<_Rp&>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:252493{
2494 if (__rhs.__state_)
2495 __rhs.__state_->__add_shared();
2496 if (__state_)
2497 __state_->__release_shared();
2498 __state_ = __rhs.__state_;
2499 return *this;
2500}
2501
2502template <>
Howard Hinnant83eade62013-03-06 23:30:192503class _LIBCPP_TYPE_VIS shared_future<void>
Howard Hinnant99be8232010-09-03 18:39:252504{
2505 __assoc_sub_state* __state_;
2506
2507public:
Howard Hinnant8c6cbb22010-09-22 14:16:262508 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552509 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252511 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2512 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192513#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552515 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252516 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262517 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552518 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252519 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192520#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252521 ~shared_future();
2522 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192523#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552525 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252526 {
2527 shared_future(std::move(__rhs)).swap(*this);
2528 return *this;
2529 }
Howard Hinnant73d21a42010-09-04 23:28:192530#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252531
2532 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252534 void get() const {__state_->copy();}
2535
Howard Hinnant8c6cbb22010-09-22 14:16:262536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552537 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252538
2539 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552541 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252542
Howard Hinnant8c6cbb22010-09-22 14:16:262543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252544 void wait() const {__state_->wait();}
2545 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252547 future_status
2548 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2549 {return __state_->wait_for(__rel_time);}
2550 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252552 future_status
2553 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2554 {return __state_->wait_until(__abs_time);}
2555};
2556
Howard Hinnant99968442011-11-29 18:15:502557template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:252558inline _LIBCPP_INLINE_VISIBILITY
2559void
Howard Hinnant8bf01dd2012-07-21 17:46:552560swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252561{
2562 __x.swap(__y);
2563}
2564
Howard Hinnant99968442011-11-29 18:15:502565template <class _Rp>
Howard Hinnant7de47902010-11-30 20:23:322566inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502567shared_future<_Rp>
2568future<_Rp>::share()
Howard Hinnante6e4d012010-09-03 21:46:372569{
Howard Hinnant99968442011-11-29 18:15:502570 return shared_future<_Rp>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:372571}
2572
Howard Hinnant99968442011-11-29 18:15:502573template <class _Rp>
Howard Hinnante6e4d012010-09-03 21:46:372574inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502575shared_future<_Rp&>
2576future<_Rp&>::share()
Howard Hinnante6e4d012010-09-03 21:46:372577{
Howard Hinnant99968442011-11-29 18:15:502578 return shared_future<_Rp&>(_VSTD::move(*this));
Howard Hinnant7de47902010-11-30 20:23:322579}
2580
Howard Hinnanta4451512010-12-02 16:45:212581#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2582
Howard Hinnant7de47902010-11-30 20:23:322583inline _LIBCPP_INLINE_VISIBILITY
2584shared_future<void>
2585future<void>::share()
2586{
Howard Hinnant0949eed2011-06-30 21:18:192587 return shared_future<void>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:372588}
2589
Howard Hinnanta4451512010-12-02 16:45:212590#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2591
Howard Hinnantbc8d3f92010-05-11 19:42:162592_LIBCPP_END_NAMESPACE_STD
2593
Jonathan Roelofs8d86b2e2014-09-05 19:45:052594#endif // !_LIBCPP_HAS_NO_THREADS
2595
Howard Hinnantbc8d3f92010-05-11 19:42:162596#endif // _LIBCPP_FUTURE